//package com.tcm.job.service;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.NumberUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.base.core.lang.DateUtils;
//import com.base.genid.generator.UidGenerator;
//import com.tcm.doctor.domain.entity.DoctorFlow;
//import com.tcm.doctor.service.DoctorFlowService;
//import com.tcm.order.entity.OrderInviterBill;
//import com.tcm.invite.service.InviteRecordService;
//import com.tcm.job.api.service.SettlementService;
//import com.tcm.job.mapper.DoctorOrderBillDayMapper;
//import com.tcm.job.mapper.OrderStoreBillDayMapper;
//import com.tcm.job.mapper.OrderStoreBillMonthMapper;
//import com.tcm.job.pojo.DoctorOrderBillDay;
//import com.tcm.job.pojo.OrderStoreBillDay;
//import com.tcm.job.pojo.OrderStoreBillMonth;
//import com.tcm.order.entity.OrderDoctorBill;
//import com.tcm.order.entity.OrderStoreBill;
//import com.tcm.order.service.task.OrderBillTaskFactory;
//import org.apache.dubbo.config.annotation.DubboReference;
//import org.apache.dubbo.config.annotation.DubboService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@DubboService(version = "0.0.1-alpha", timeout = 6000, retries = 3, delay = -1)
//public class SettlementServiceImpl implements SettlementService {
//
//    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
//    private UidGenerator uidGenerator;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private OrderBillService orderBillService;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private OrderStoreBillService orderStoreBillService;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private OrderBillTaskFactory orderBillTaskFactory;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private DoctorFlowService doctorFlowService;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private OrderInviterBillService orderInviterBillService;
//
//    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
//    private InviteRecordService inviteRecordService;
//
//    @Autowired
//    private DoctorOrderBillDayMapper doctorOrderBillDayMapper;
//
//    @Autowired
//    private OrderStoreBillDayMapper orderStoreBillDayMapper;
//
//    @Autowired
//    private OrderStoreBillMonthMapper orderStoreBillMonthMapper;
//
//    /**
//     * 机器号，后期优化需要写入到配置中
//     */
//    private final String machineNo = "01";
//    /**
//     * 环境编号，后期需要优化需要写入到配置中
//     */
//    private final String env = "1";
//
//    /**
//     * 药房日结算
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void settlementStoreOrder() {
//        //查询所有未结算订单
//        String startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toString();
//        String endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toString();
//        List<OrderStoreBill> orderStoreBills = orderStoreBillService.outstandingOrders(startTime, endTime);
//        if (ObjectUtil.isEmpty(orderStoreBills)) {
//            return;
//        }
//        //数据存在map内
//        //分组,收集添加日结记录数据源
//        Map<Long, List<OrderStoreBill>> orderStoreBillMap = orderStoreBills.stream()
//                .collect(Collectors.groupingBy(OrderStoreBill::getStoreId));
//        if(ObjectUtil.isEmpty(orderStoreBillMap)){
//            return;
//        }
//        for(Long key : orderStoreBillMap.keySet()){
//            List<OrderStoreBill> orderStoreBillList = orderStoreBillMap.get(key);
//            OrderStoreBillDay orderStoreBillDay = new OrderStoreBillDay();
//            for(OrderStoreBill orderStoreBill : orderStoreBillList){
//                orderStoreBillDay.setTotalDrugsMoney(
//                        NumberUtil.add(orderStoreBillDay.getTotalDrugsMoney(),orderStoreBill.getTotalDrugsMoney())
//                );
//                orderStoreBillDay.setDrugsCommission(
//                        NumberUtil.add(orderStoreBillDay.getDrugsCommission(),orderStoreBill.getDrugsCommission())
//                );
//                orderStoreBillDay.setDecoctingFee(
//                        NumberUtil.add(orderStoreBillDay.getDecoctingFee(),orderStoreBill.getDecoctingFee())
//                );
//                orderStoreBillDay.setProcessCost(
//                        NumberUtil.add(orderStoreBillDay.getProcessCost(),orderStoreBill.getProcessCost())
//                );
//                orderStoreBillDay.setDeductPointMoney(
//                        NumberUtil.add(orderStoreBillDay.getDeductPointMoney(),orderStoreBill.getDeductPointMoney())
//                );
//
//                orderStoreBillDay.setTotalMoney(
//                        NumberUtil.add(orderStoreBillDay.getTotalMoney(), orderStoreBill.getTotalMoney())
//                );
//            }
//            orderStoreBillDay.setStoreId(key);
//            orderStoreBillDay.setCreateTime(DateUtil.date());
//            orderStoreBillDay.setUpdateTime(DateUtil.date());
//            orderStoreBillDay.setState("0");
//            orderStoreBillDayMapper.insert(orderStoreBillDay);
//        }
//
//
//
//    }
//
//    /**
//     * 药房月结算
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void settlementMonthStoreOrder() {
//        //查询所有未结算订单
//        String startTime = DateUtils.getPrevMonthFirstDay();
//        String endTime = DateUtils.getPrevMonthLastDay();
//        List<OrderStoreBill> orderStoreBills = orderStoreBillService.outstandingOrders(startTime, endTime);
//        if (ObjectUtil.isEmpty(orderStoreBills)) {
//            return;
//        }
//
//        //数据存在map内
//        //分组,收集添加日结记录数据源
//        Map<Long, List<OrderStoreBill>> orderStoreBillMap = orderStoreBills.stream()
//                .collect(Collectors.groupingBy(OrderStoreBill::getStoreId));
//        if(ObjectUtil.isEmpty(orderStoreBillMap)){
//            return;
//        }
//        for(Long key : orderStoreBillMap.keySet()){
//            List<OrderStoreBill> orderStoreBillList = orderStoreBillMap.get(key);
//            OrderStoreBillMonth orderStoreBillMonth = new OrderStoreBillMonth();
//            for(OrderStoreBill orderStoreBill : orderStoreBillList){
//                orderStoreBillMonth.setTotalDrugsMoney(
//                        NumberUtil.add(orderStoreBillMonth.getTotalDrugsMoney(),orderStoreBill.getTotalDrugsMoney())
//                );
//                orderStoreBillMonth.setDrugsCommission(
//                        NumberUtil.add(orderStoreBillMonth.getDrugsCommission(),orderStoreBill.getDrugsCommission())
//                );
//                orderStoreBillMonth.setDecoctingFee(
//                        NumberUtil.add(orderStoreBillMonth.getDecoctingFee(),orderStoreBill.getDecoctingFee())
//                );
//                orderStoreBillMonth.setProcessCost(
//                        NumberUtil.add(orderStoreBillMonth.getProcessCost(),orderStoreBill.getProcessCost())
//                );
//                orderStoreBillMonth.setDeductPointMoney(
//                        NumberUtil.add(orderStoreBillMonth.getDeductPointMoney(),orderStoreBill.getDeductPointMoney())
//                );
//
//                orderStoreBillMonth.setTotalMoney(
//                        NumberUtil.add(orderStoreBillMonth.getTotalMoney(), orderStoreBill.getTotalMoney())
//                );
//            }
//            orderStoreBillMonth.setStoreId(key);
//            orderStoreBillMonth.setCreateTime(DateUtil.date());
//            orderStoreBillMonth.setUpdateTime(DateUtil.date());
//            orderStoreBillMonth.setState("0");
//            orderStoreBillMonthMapper.insert(orderStoreBillMonth);
//        }
//
//    }
//
//
//
//    /**
//     * 医生日结算
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void settlementDoctorOrder() {
//
//        //查询所有待结算的医生结算单
//        String startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toString();
//        String endTime = LocalDateTime.of(LocalDate.now(),LocalTime.MAX).toString();
//        List<OrderDoctorBill> orderDoctorBills = orderBillTaskFactory.outstandingOrders(startTime, endTime);
//        if (ObjectUtil.isNull(orderDoctorBills)) {
//            return;
//        }
//
//        //数据存在map内
//        Map<Long, DoctorFlow> doctorFlowMap = new HashMap<>();
//        for (OrderDoctorBill orderDoctorBill : orderDoctorBills) {
//            if (ObjectUtil.isNotNull(doctorFlowMap.get(orderDoctorBill.getUserId()))) {
//                DoctorFlow doctorFlow = doctorFlowMap.get(orderDoctorBill.getUserId());
//                doctorFlow.setMoney(
//                        NumberUtil.add(orderDoctorBill.getTotalCommission(), doctorFlow.getMoney())
//                );
//                doctorFlowMap.put(orderDoctorBill.getUserId(),doctorFlow);
//            } else {
//                DoctorFlow doctorFlow = new DoctorFlow();
//                doctorFlow.setId(uidGenerator.getUID());
//                doctorFlow.setUserId(orderDoctorBill.getUserId());
//                doctorFlow.setType(new Short("5"));
//                doctorFlow.setMoney(orderDoctorBill.getTotalCommission());
//                doctorFlow.setMemo("佣金提成");
//                doctorFlowMap.put(orderDoctorBill.getUserId(),doctorFlow);
//            }
//            orderDoctorBill.setState("2");
//            orderBillTaskFactory.update(orderDoctorBill);
//
//
//        }
//        //分组,收集添加日结记录数据源
//        Map<Long, List<OrderDoctorBill>> doctorOrderBillMap = orderDoctorBills.stream()
//                .collect(Collectors.groupingBy(OrderDoctorBill::getUserId));
//        if(ObjectUtil.isEmpty(doctorOrderBillMap)){
//            return;
//        }
//        for(Long key : doctorOrderBillMap.keySet()){
//            List<OrderDoctorBill> orderDoctorBillList = doctorOrderBillMap.get(key);
//            DoctorOrderBillDay doctorOrderBillDay = new DoctorOrderBillDay();
//            for(OrderDoctorBill orderDoctorBill : orderDoctorBillList){
//                doctorOrderBillDay.setBillingMoney(
//                        NumberUtil.add(doctorOrderBillDay.getBillingMoney(), orderDoctorBill.getBillingMoney())
//                );
//                doctorOrderBillDay.setBillingCommission(
//                        NumberUtil.add(doctorOrderBillDay.getBillingCommission(), orderDoctorBill.getBillingCommission())
//                );
//                doctorOrderBillDay.setDrugsCommission(
//                        NumberUtil.add(doctorOrderBillDay.getDrugsCommission(), orderDoctorBill.getDrugsCommission())
//                );
//                doctorOrderBillDay.setDrugsMoney(
//                        NumberUtil.add(doctorOrderBillDay.getDrugsMoney(), orderDoctorBill.getDrugsMoney())
//                );
//                doctorOrderBillDay.setServiceCommission(
//                        NumberUtil.add(doctorOrderBillDay.getServiceCommission(), orderDoctorBill.getServiceCommission())
//                );
//                doctorOrderBillDay.setServiceMoney(
//                        NumberUtil.add(doctorOrderBillDay.getServiceMoney(), orderDoctorBill.getTotalServiceMoney())
//                );
//                doctorOrderBillDay.setTotalCommission(
//                        NumberUtil.add(doctorOrderBillDay.getTotalCommission(), orderDoctorBill.getTotalCommission())
//                );
//            }
//            doctorOrderBillDay.setUserId(key);
//            doctorOrderBillDay.setCreateTime(DateUtil.date());
//            doctorOrderBillDay.setUpdateTime(DateUtil.date());
//            doctorOrderBillDay.setState("0");
//            doctorOrderBillDayMapper.insert(doctorOrderBillDay);
//        }
//
//        for (Map.Entry<Long, DoctorFlow> entry : doctorFlowMap.entrySet()) {
//            //添加医生资金明细
//            doctorFlowService.insertRecord(entry.getValue());
//        }
//
//
//    }
//
//
//    /**
//     * 业务员结算
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void settlementInviterOrder() {
//
//        //查询所有未结算的订单
//        List<OrderInviterBill> orderInviterBills = orderInviterBillService.outstandingOrders();
//        if (ObjectUtil.isNull(orderInviterBills)) {
//            return;
//        }
//        //数据存在map内
//        Map<Long, List<OrderInviterBill>> longListMap = new HashMap<>();
//        for (OrderInviterBill orderInviterBill : orderInviterBills){
//            if (ObjectUtil.isNotNull(longListMap.get(orderInviterBill.getUserId()))) {
//                List<OrderInviterBill> orderInviterBillList = longListMap.get(orderInviterBill.getUserId());
//                orderInviterBillList.add(orderInviterBill);
//                longListMap.put(orderInviterBill.getUserId(), orderInviterBillList);
//            }else{
//                List<OrderInviterBill> orderInviterBillList = new ArrayList<>();
//                orderInviterBillList.add(orderInviterBill);
//                longListMap.put(orderInviterBill.getUserId(), orderInviterBillList);
//            }
//        }
//
//        for (Map.Entry<Long, List<OrderInviterBill>> entry : longListMap.entrySet()) {
//            //添加邀请人明细
//            inviteRecordService.updateRecord(entry.getValue(),entry.getKey());
//        }
//
//    }
//}
